package timing.ukulele.form.api;

import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.form.FormTemplateData;
import timing.ukulele.form.persistent.FormTemplate;
import timing.ukulele.form.service.FormTemplateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/form")
public class FormApi {
    private final FormTemplateService templateService;

    public FormApi(FormTemplateService templateService) {
        this.templateService = templateService;
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取模板信息")
    public ResponseResult<FormTemplateData> findById(
            @ApiParam(value = "id", required = true, example = "1") @PathVariable("id") Long id) {
        FormTemplate template = templateService.getBaseMapper().getTemplate(id);
        if (template != null) {
            FormTemplateData data = new FormTemplateData();
            BeanUtils.copyProperties(template, data);
            return ResponseResult.success(data);
        }
        return ResponseResult.success();
    }

    @GetMapping("/getBySystemId/{systemId}")
    @ApiOperation("分页获取事项列表")
    public ResponseResult<List<FormTemplateData>> getBySystemId(
        @ApiParam(value = "事项编号", required = true) @PathVariable("systemId") Long systemId) {
        List<FormTemplateData> formTemplateDatas = templateService.getBySystemId(systemId);
        return ResponseResult.success(formTemplateDatas);
    }
}
